package ua.kiev.tosik.price.modules.clients.gui.prices;

import org.apache.log4j.Logger;
import ua.kiev.tosik.price.conf.AppConfig;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class PricesToolBar {

	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.gui.prices");
	private JToolBar toolBar;
	private JButton loadFileButton;
	private JButton closeButton;
	private JButton deleteAllButton;
	private JFileChooser fileChooser;
	private ModuleConfig config;
	private PricesDialog dialog;

	public PricesToolBar(PricesDialog dialog) {
		this.dialog = dialog;
		config = ModuleConfig.getInstance();

		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setBorder(BorderFactory.createLineBorder(Color.gray, 1));

		loadFileButton = new JButton(config.get("clients.editor.prices.button.upload.text"));
		loadFileButton.setIcon( new ImageIcon(config.get("clients.editor.prices.button.upload.icon")) );

		closeButton = new JButton(config.get("clients.editor.prices.button.close.text"));
		closeButton.setIcon( new ImageIcon(config.get("clients.editor.prices.button.close.icon")) );

		deleteAllButton = new JButton(config.get("clients.editor.prices.button.delete.text"));
		deleteAllButton.setIcon( new ImageIcon(config.get("clients.editor.prices.button.delete.icon")) );

		toolBar.add(loadFileButton);
		toolBar.add(deleteAllButton);
		toolBar.add(closeButton);

		loadFileButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				loadFile();
			}
		});
		closeButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		deleteAllButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteAll();
			}
		});

	}

	public JToolBar getToolBar() {
		return toolBar;
	}

	public synchronized void setEnableButtons(boolean enabled) {
		loadFileButton.setEnabled(enabled);
		closeButton.setEnabled(enabled);
		deleteAllButton.setEnabled(enabled);
	}

	public void close() {
		dialog.close();
	}

	private void deleteAll() {
		dialog.deletePricesFilesAll();
	}

	private void loadFile() {
		if (fileChooser == null) {
			logger.debug("Initialize FileOpen dialog");
			fileChooser = new JFileChooser();
			fileChooser.setDialogTitle(config.get("clients.editor.prices.upload.fileChooser.title"));
			fileChooser.setMultiSelectionEnabled(false);
			FileFilter filter = new FileNameExtensionFilter("Excel файл (*.xls)", "xls");
			fileChooser.addChoosableFileFilter(filter);
		}

		if ( !AppConfig.getInstance().get("app.lastDir").equals("") ) {
			fileChooser.setCurrentDirectory(new File(AppConfig.getInstance().get("app.lastDir")));
		}

		int returnVal = fileChooser.showOpenDialog(dialog.getDialog());
		if ( returnVal == JFileChooser.APPROVE_OPTION ) {
			AppConfig.getInstance().set("app.lastDir", fileChooser.getCurrentDirectory().getAbsolutePath());
			File newFile = fileChooser.getSelectedFile();
			dialog.openFileUploadDialog(newFile);
			// loadFileButton.setEnabled(false);
			/*
			ExcelParseWorker parseWorker = new ExcelParseWorker(newFile);
			parseWorker.addPropertyChangeListener(StatusBar.getInstance());
			parseWorker.addPropertyChangeListener(this);
			parseWorker.execute();
			*/
		}
	}

}
